package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.CustomerQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.EmployeeService;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private EmployeeService employeeService;


    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISystemDictionaryItemService iSystemDictionaryItemService;


    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/list";
    }

                                    //customer:potentialList
    //潜在客户页面
    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        qo.setStatus(Customer.STATUS_COMMON);
        //普通销售员登录进来只能看到自己客户 ,看不到别人的客户,管理员或者市场经理才可以看所有
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("ADMIN" ) || subject.hasRole("Market_Manager"))){
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);

        //只查询带有市场专员或市场经理角色的员工
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
       model.addAttribute("sellers",sellers);


       //职业下拉框的回显
        List<SystemDictionaryItem> jobs = iSystemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);

        //查询来源
        List<SystemDictionaryItem> sources = iSystemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources",sources);
        return "customer/potentialList";
    }



    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/list";
    }


    //失败客户页面
    @RequestMapping("/failList")
    public String failList(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/list";
    }







    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        if (customer.getId() != null) {
            customerService.update(customer);
        }else {
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
